import plotly.express as px
import plotly.graph_objects as go


def create_3d_visualization(df):
    """
    创建交互式3D地理可视化
    """
    # 创建基础3D散点图
    fig = px.scatter_3d(
        df,
        x='longitude',
        y='latitude',
        z='price',
        color='price_category',
        hover_name='name',
        hover_data=['neighbourhood_group', 'room_type', 'minimum_nights'],
        title='纽约Airbnb房源3D分布图 (2019)',
        color_discrete_sequence=px.colors.qualitative.Vivid,
        opacity=0.7,
        height=800
    )

    # 更新布局设置
    fig.update_layout(
        scene=dict(
            xaxis_title='经度',
            yaxis_title='纬度',
            zaxis_title='价格(美元)',
            zaxis=dict(range=[0, max(df['price'])])
        ),
        legend_title_text='价格分类',
        margin=dict(l=0, r=0, b=0, t=40),
        scene_camera=dict(
            eye=dict(x=1.5, y=1.5, z=0.8)  # 设置初始视角
        )
    )

    # 添加纽约地图背景
    fig.update_traces(marker=dict(size=4, symbol='circle'))
    fig.update_layout(scene=dict(
        xaxis=dict(showbackground=True, backgroundcolor="#0E1117"),
        yaxis=dict(showbackground=True, backgroundcolor="#0E1117"),
        zaxis=dict(showbackground=True, backgroundcolor="#0E1117"),
    ))

    return fig